<?php

namespace app\api\model;

use think\Model;
use think\Db;

class HotwordConfig extends Model 
{
	protected $_state = ['1'=>'模型已生成', '2'=>'模型生成中', '3'=>'热词已更新']; 
	/**
	 * 获取配置列表
	 * @param  array   $param  [description]
	 */
	public function getDataList()
	{
		$list = $this->select();
		$data = array();
        foreach ($list as $key => $val) {
			switch($val['id'])
			{
				case '2': $data[$val['name']] = date('Y-m-d H:i:s',$val['value']);
				continue 2;
				case '3': $data[$val['name']] = $this->_state[$val['value']];
				$data['STATE'] = $val['value'];
				continue 2;
				default : $data[$val['name']] = $val['value'];
			}
		}
		$data['WORD_COUNT'] = Db::name('hot_word')->where(['is_delete' => 0])->sum('total');
        return $data;
	}
	
	/**
	 * 更新模型生成状态
	 */
	public function updateEnableState($param)
	{
		$list = [
			['id' => 2, 'value' => time()],
			['id' => 3, 'value' => $param]
		];
		switch($param)
		{
			case 2:$list[] = ['id' => 5, 'value' => time()];break;
			case 1:$list[] = ['id' => 5, 'value' => 0];break;
			default :break;
		}

		if($this->get(3)->saveAll($list)){
			$data = $this->getDataList();
			cache('DB_HW_CONFIG_DATA', null);
			cache('DB_HW_CONFIG_DATA', $data);
		}
	}
}
